﻿using HandyControl.Controls;
using HandyControl.Data;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLinePacking.Common;
using ZoneLinePacking.Models.Dto;
using ZoneLinePacking.Services.DB;
using ZoneLinePacking.Services.Message;
using ZoneLinePacking.ViewModelBase;
using ZoneLinePacking.Views;

namespace ZoneLinePacking {
    public class MainWindowModel : NavigationViewModel {
        public MainWindowModel(
            IRegionManager regionManager,
            IDialogService dialog,
            IMessageService messageBox,
            IEventAggregator eventAggregator,
            IDbOperate<IFreeSqlMainContext> mainDbOperate)
            : base(regionManager, dialog, messageBox, eventAggregator, mainDbOperate) {


        }

        private string _title = AppSession.AppactionName;

        public string Title {
            get { return _title; }
            set { _title = value; RaisePropertyChanged(); }
        }

        private bool _isProduction = true;
        /// <summary>
        /// 是否是生产系统 
        /// </summary>
        public bool IsProduction {
            get { return _isProduction; }
            set { _isProduction = value; RaisePropertyChanged(); }
        }

        private int _SelectedIndex;
        public int SelectedIndex {
            get { return _SelectedIndex; }
            set { _SelectedIndex = value; RaisePropertyChanged(); }
        }

        private MenuItemDto _SelectedItem;
        public MenuItemDto SelectedItem {
            get { return _SelectedItem; }
            set {
                if (value == null || string.IsNullOrWhiteSpace(value.Code))
                    return;
            }
        }

        public DelegateCommand<FunctionEventArgs<object>> SwitchItemCmd => new DelegateCommand<FunctionEventArgs<object>>(obj => {
            if (obj.Info is SideMenuItem item) {
                _regionManager.Regions[PrismRegionManager.MainViewRegionName].RequestNavigate(item.Name, arg => {
                    //没有导航成功 
                    if (arg.Result == false) {
                        if (arg.Error != null) {
                            _message.GrowError($"导航失败，pageParams：{item.Name}，errorInfo：{arg.Error.Message}");
                        }
                    } else {
                        Console.WriteLine("导航成功");
                    }
                });
            }
        });

        protected override void OnViewLoaded() {
            var arg = new FunctionEventArgs<object>(null, null);
            arg = new FunctionEventArgs<object>(null, null) { Info = new SideMenuItem() { Name = nameof(MainPackingView) } };
            SwitchItemCmd.Execute(arg);
            base.OnViewLoaded();
        }

    }
}
